import { ajaxAsync } from 'vue-xiaobu-utils'
import ArticleListModel from '../models/ArticleListModel'
import { isUserLogin } from '@/utils/UserBeanUtils'
/**
 * 获取文章列表
 * @param {Number}  pageNo 页码
 * @param {Number} pageSize 每页条数
 */

async function getArticleList(pageNo, pageSize) {
  let articleList = []
  let sendObj = {
    GROUP_ID: window.GROUP_ID,
    PAGE_CAPACITY: pageSize,
    PAGE_INDEX: pageNo
  }
  let res = await ajaxAsync('/city_news/list', sendObj)
  if (res.COUNT > 0) {
    res.LIST.map(item => {
      articleList.push(
        new ArticleListModel(
          item.NEWS_ID,
          item.TITLE,
          item.SUB_TITLE,
          item.DESCRIPTION,
          item.BIG_PIC,
          item.MIDDLE_PIC,
          item.SMALL_PIC,
          item.GOTO,
          item.START_TIME,
          item.END_TIME,
          item.PAGE_VIEWS,
          item.PRAISES,
          item.COMMENTS,
          item.FORWARDS,
          item.FAVOURS,
          item.CREATE_TIME,
          item.UPDATE_TIME,
          item.STICK
        )
      )
    })
  }
  let data = {
    count: res.COUNT,
    articleList: articleList
  }
  return data
}

/**
 * 未读消息的获取
 */
async function getMessage() {
  if (isUserLogin()) {
    try {
      let res = await ajaxAsync('/user/message/unread_count', {}, 2)
      return res.COUNT
    } catch (err) {
      let code = err.error.response.status
      if (code === 403) return 0
    }
  } else {
    return 0
  }
}

export { getArticleList, getMessage }
